package org.celery.modules.productSource.controller;

import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.client.APIId;
import com.alibaba.ocean.rawsdk.common.SDKResult;
import com.alibaba.trade.param.AlibabaTradeGetSellerOrderListParam;
import com.alibaba.trade.param.AlibabaTradeGetSellerOrderListResult;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.celery.common.config.AlibabaProperties;
import org.celery.modules.productSource.entity.*;
import org.celery.modules.productSource.service.IProductSourceService;
import org.celery.modules.shop.entity.Shop;
import org.celery.modules.shop.service.IShopService;
import org.celery.modules.ticket.entity.Ticket;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 订单表 控制器
 *
 * @author ly
 * @since 2023-06-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("product-source/source")
@Api(value = "商品源", tags = "商品源表接口")
public class TestController {

    private final AlibabaProperties alibabaProperties;

    private final IShopService shopService;

    private final IProductSourceService productSourceService;

    /**
     * 获取分页数据
     */
    @GetMapping("/get_produce_source_page")
    public R<HashMap<String, Object>> ProductSourceServiceByPage(AlibabaProductGetListParam alibabaProductGetListParam, @RequestParam Long shopIdStatic) throws ExecutionException, InterruptedException {
        return R.data(productSourceService.getProductSourceServiceByPage(alibabaProductGetListParam, shopIdStatic).get());
    }


    /**
     * 获取分页数据(查询自己所有的产品）
     */
    @GetMapping("/get_produce_source_page_self")
    public R<AlibabaProductGetResult> ProductSourceServiceByPageSelf(AlibabaProductGetParam alibabaProductGetParam) {
        return R.data(productSourceService.getProductSourceServiceByPageSelf(alibabaProductGetParam));
    }

    /**
     * 获取分页数据(获取ticket表 -》只能查询自己所有的产品，因为只能单条查询，所以通过循环的方式进行遍历）
     */
    @GetMapping("/get_produce_source_page_ticket_self")
    public R<HashMap<String, Object>> getCurrentProductSourceServiceByPageSelf(Ticket ticket, Query query) {
        return R.data(productSourceService.getCurrentProductSourceServiceByPageSelf(ticket, query));
    }

    /**
     * 点击查询获取分页数据(获取ticket表 -》只能查询自己所有的产品，因为只能单条查询，所以通过循环的方式进行遍历）
     */
    @GetMapping("/get_produce_source_page_ticket_id_owner")
    public R<HashMap<String, Object>> getCurrentProductSourceServiceByPageSelfById(String LongId) {
        return R.data(productSourceService.getCurrentProductSourceServiceByPageSelfById(LongId));
    }

    /**
     * 点击查询买家商品
     */
    @GetMapping("/get_produce_source_page_ticket_id")
    public R<HashMap<String, Object>> getMyCurrentProductSourceServiceByPageSelfById(String LongId, AlibabaProductGetListParam alibabaProductGetListParam, Long  shopIdStatic) {
        return R.data(productSourceService.getMyCurrentProductSourceServiceByPageSelfById(LongId, alibabaProductGetListParam, shopIdStatic));
    }

    /**
     * 获取分页数据(根据商品ID列表获取商品(卖家)）
     */
    @GetMapping("/get_produce_source_page_seller")
    public R<AlibabaProductGetByIdListResult> ProductSourceServiceByPageSeller(AlibabaProductGetByIdListParam alibabaProductGetByIdListParam, Long shopIdStatic) {
        return R.data(productSourceService.productSourceServiceByPageSeller(alibabaProductGetByIdListParam, shopIdStatic));
    }

    /**
     * 异步操作
     */
    @GetMapping("/async_save_date")
    public R<Future<Boolean>> synchronizeProductSources(@RequestParam Long shopIdStatic) throws ExecutionException, InterruptedException {
        Future<Boolean> booleanFuture = productSourceService.synchronizeProductSources(shopIdStatic);
        return R.status(booleanFuture.get());
    }

    /**
     * 详情
     */
    @GetMapping("/test")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "测试接口", notes = "")
    public R<Object> detail() {
        ApiExecutor apiExecutor = new ApiExecutor(alibabaProperties.getAppKey(), alibabaProperties.getAppSecret());
        AlibabaProductGetListParam param = new AlibabaProductGetListParam();
        param.setOceanApiId(new APIId("com.alibaba.product", "alibaba.product.list.get", 1));
        param.setPageNo(1);
        param.setPageSize(2);
        Shop shop = shopService.getOne(Wrappers.<Shop>lambdaQuery().eq(Shop::getId, "1670753159815319553")
                .eq(Shop::getIsDeleted, 0));
        SDKResult<AlibabaProductGetListResult> result = apiExecutor.execute(param, shop.getAccessToken());
        return R.data(result);
    }

    @GetMapping("/test2")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "测试接口", notes = "")
    public R<Object> detail2() {
        ApiExecutor apiExecutor = new ApiExecutor(alibabaProperties.getAppKey(), alibabaProperties.getAppSecret());
        AlibabaTradeGetSellerOrderListParam param = new AlibabaTradeGetSellerOrderListParam();
        param.setOceanApiId(new APIId("com.alibaba.trade", "alibaba.trade.ec.getOrderList.sellerView", 1));
        param.setPage(1);
        param.setPageSize(100);

        Shop shop = shopService.getOne(Wrappers.<Shop>lambdaQuery().eq(Shop::getId, "1670753159815319553")
                .eq(Shop::getIsDeleted, 0));
        SDKResult<AlibabaTradeGetSellerOrderListResult> result = apiExecutor.execute(param, shop.getAccessToken());
        return R.data(result);
    }

    @GetMapping("/test3")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "测试接口", notes = "")
    public R<Object> detail3() {
        ApiExecutor apiExecutor = new ApiExecutor(alibabaProperties.getAppKey(), alibabaProperties.getAppSecret());
        AlibabaProductGetParam param = new AlibabaProductGetParam();
        param.setOceanApiId(new APIId("com.alibaba.product", "alibaba.product.get", 1));
        param.setProductID(123456L);
        Shop shop = shopService.getOne(Wrappers.<Shop>lambdaQuery().eq(Shop::getId, "1670753159815319553")
                .eq(Shop::getIsDeleted, 0));
        SDKResult<AlibabaProductGetResult> result = apiExecutor.execute(param, shop.getAccessToken());
        return R.data(result);
    }

    /**
     * 点击商品源添加链接
     */
    @GetMapping("/add_base_url")
    public R<Boolean> addProductSourceUrl(@RequestParam Long productId ) {
        return R.status(productSourceService.addProductSourceUrl(productId));
    }

    /**
     * 查询同步数据
     */
    @PostMapping("data_list_search")
    public R<List<ProductSource>> dataListSearch(@RequestBody List<Long> ids) {
        return R.data(productSourceService.dataListSearch(ids));
    }
}
